package com.wman

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.wman.learn.funcation.MyViewModel
import com.wman.mvp.ui.activity.welcome.MvpWelcomeActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var viewModel: MyViewModel = ViewModelProvider(this).get(MyViewModel::class.java)
        viewModel.number.observe(this, Observer { tvText.text = it.toString() })

        tvText.text = "0"
        buttomPlus.setOnClickListener {
            viewModel.modifyNumber(1)
        }

        buttomMinus.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                viewModel.modifyNumber(-1)
            }
        })

        buttomToMvpActivity.setOnClickListener {
            var intent: Intent = Intent(this, MvpWelcomeActivity::class.java)
            startActivity(intent)
        }

        getSharedPreferences("WMAN", Context.MODE_PRIVATE).edit(false) {
            putBoolean("aaa", true)
            putString("bbb", "STRING")
        }
        getSharedPreferences("WMAN", Context.MODE_PRIVATE).modify {
            putBoolean("aaa", true)
            putString("bbb", "STRING")
        }
    }

    /**
     * 高阶函数的实例
     */
    fun SharedPreferences.modify(block: SharedPreferences.Editor.() -> Unit) {
        val editor = edit()
        editor.block()
        editor.apply()
    }

    open class User {
        private var userName: String = "Czh"
            get() = field
            set(value) {
                field = value
            }

        inner class UserName {
        }
    }

    class Baby : User() {

    }

}